<?php

namespace App\Repository\Orders;

use App\Jobs\OrderSubmit;
use App\Models\Orders\OrdersModel as model;
use App\Repository\BaseRepository;
use App\Repository\Order\OrderInfoRepository;
use App\Repository\Order\OrderLogRepository;
use App\Repository\Order\OrderProductRepository;
use App\Repository\Orders\OrdersUserOldRepository as newOrdersInfo;
use App\Repository\Product\GoodsRepository;

class OrdersOldRepository extends BaseRepository
{
    public function __construct()
    {
        $this->model = new model();
        $this->modelInfo = new OrderInfoRepository();
        $this->logModel = new OrderLogRepository;
        $this->productModel = new OrderProductRepository;
        $this->goodsModel = new GoodsRepository();
        $this->newOrdersInfo = new newOrdersInfo();
        parent::__construct();
    }

    //列表
    public function index($where)
    {
        $cron = $where;
        $cron['order'] = ['orderby' => 'orders_user.id', 'orderdir' => 'desc'];
        // $cron['where'] = $where;
        // var_dump($cron,111111111);
        // var_dump($cron, 444444444);
        // DB::connection()->enableQueryLog();
        $data = $this->search($this->model, $cron);
        // $logs = DB::getQueryLog();
        // dd($logs);
        // if (!empty($data['data'])) {
        //   foreach ($data['data'] as $key => &$val) {
        //     if (!empty($val['channel_error_josn'])) {
        //       $val['channel_error_josn'] = json_decode($val['channel_error_josn'], true);
        //     }
        //   }
        // }
        return $data;
    }

    //添加  数据
    public function add($data)
    {

        //必填 字段
        // $data['orders_user_id'] = $userId;
        if (!empty($data['orderno'])) {
            $data['ordersno'] = $data['orderno'];
        }
        $mustField = [
            'ordersno',
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'ordersno' => '订单编号不能为空',
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        // if (!empty($insertData['status'])) {
        //   $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
        // }
        // if (!empty($insertData['source'])) {
        //   $insertData['source_type'] = $this->model->getOrderSource($insertData['source']);
        // }
        // 订单表插入
        if (!empty($insertData['status'])) {
            $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
        }
        if (!empty($insertData['shop_id_str'])) {
            // $insertData['shop_id_str'] = substr_replace('0.', '', $insertData['shop_id_str'], 1);
            $insertData['shop_id_str'] = substr_replace($insertData['shop_id_str'], '', 0, 2);
        }
        if (empty($insertData['shop_id_str'])) {
            $insertData['shop_id_str'] = $insertData['shop_id'];
        }
        if (!empty($insertData['sup_goods_id_str'])) {
            // $insertData['sup_goods_id_str'] = substr_replace('0.', '', $insertData['sup_goods_id_str'], 1);
            $insertData['sup_goods_id_str'] = substr_replace($insertData['sup_goods_id_str'], '', 0, 2);
        }
        if (empty($insertData['sup_goods_id_str'])) {
            $insertData['sup_goods_id_str'] = $insertData['goods_id'];
        }
        if (!empty($insertData['shop_name_str'])) {
            // $insertData['shop_name_str'] = substr_replace('0.', '', $insertData['shop_name_str'], 1);
            $insertData['shop_name_str'] = substr_replace($insertData['shop_name_str'], '', 0, 2);
        }
        if (empty($insertData['shop_name_str'])) {
            $insertData['shop_name_str'] = $insertData['shop_name'];
        }
        $insertData['created_at'] = date('Y-m-d H:i:s');
        $insertData['one_product_code'] = $insertData['product_code'];
        $insertData['one_product_name'] = $insertData['product_name'];
        $insertData['one_product_id'] = $insertData['product_id'];
        $ordersId = $this->model->insertGetId($insertData);
        // 先添加 orders 然后在添加 orders_info
        $data['orders_id'] = $ordersId;
        // 记录用户的信息
        $userArr = $this->newOrdersInfo->add($data);
        $userId = 0;
        if (!empty($userArr['code']) && $userArr['code'] == 200) {
            $userId = $userArr['data']['id'];
        }
        if (empty($userArr['code']) || $userArr['code'] != 200) {
            return $userArr;
        }
        // 将 order里面存入 信息
        $res = $this->model->where(['id' => $ordersId])->update(['orders_user_id' => $userId]);

        // 添加log 日志
        //操作日志记录
        $logDdata = [
            'orderno' => $insertData['ordersno'],
            'shop_str' => $insertData['shop_str'],
            'shop_id' => $insertData['shop_id'],
            'remark' => date('Y-m-d H:i:s') . '提交信息',
        ];
        $this->logModel->add($logDdata); // 存入操作日志
        // 添加 产品验证
        $productData = [
            'username' => $data['pay_name'],
            'idcard' => $data['pay_identity'],
            'phone' => $data['pay_phone'],
            'address' => $data['post_address'],
            'product_id' => !empty($data['product_id']) ? $data['product_id'] : '0',  //产品的id
            'shop_id' => $data['shop_id'],
        ];
        $this->productModel->add($productData); // 为了验证
        if ($res) {
            // 插入到队列中
            $this->upThree($insertData['ordersno']);
            return $this->returnMsg(200, '上传成功');
        }
        return $this->returnMsg(1, '上传失败');
    }

    public function addEdit($data)
    {

        //必填 字段
        // $data['orders_user_id'] = $userId;
        if (!empty($data['orderno'])) {
            $data['ordersno'] = $data['orderno'];
        }
        $mustField = [
            'ordersno',
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'ordersno' => '订单编号不能为空',
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        // 订单表插入
        if (!empty($insertData['status'])) {
            $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
        }
        if (!empty($insertData['shop_id_str'])) {
            // $insertData['shop_id_str'] = substr_replace('0.', '', $insertData['shop_id_str'], 1);
            $insertData['shop_id_str'] = substr_replace($insertData['shop_id_str'], '', 0, 2);
        }
        if (empty($insertData['shop_id_str'])) {
            $insertData['shop_id_str'] = $insertData['shop_id'];
        }
        if (!empty($insertData['sup_goods_id_str'])) {
            // $insertData['sup_goods_id_str'] = substr_replace('0.', '', $insertData['sup_goods_id_str'], 1);
            $insertData['sup_goods_id_str'] = substr_replace($insertData['sup_goods_id_str'], '', 0, 2);
        }
        if (empty($insertData['sup_goods_id_str'])) {
            $insertData['sup_goods_id_str'] = $insertData['goods_id'];
        }
        if (!empty($insertData['shop_name_str'])) {
            // $insertData['shop_name_str'] = substr_replace('0.', '', $insertData['shop_name_str'], 1);
            $insertData['shop_name_str'] = substr_replace($insertData['shop_name_str'], '', 0, 2);
        }
        if (empty($insertData['shop_name_str'])) {
            $insertData['shop_name_str'] = $insertData['shop_name'];
        }

        $insertData['created_at'] = date('Y-m-d H:i:s');
        $res = $this->model->insertGetId($insertData);
        // 修改用户最后一次显示的数据
        $this->newOrdersInfo->edit(['id' => $insertData['orders_user_id']], ['orders_id' => $res, 'is_trun' => 1]);
        // 添加 产品验证
        // $productData = [
        //   'username' => $data['pay_name'],
        //   'idcard' => $data['pay_identity'],
        //   'phone' => $data['pay_phone'],
        //   'address' => $data['post_address'],
        //   'product_id' => !empty($data['product_id']) ? $data['product_id'] : '0',  //产品的id
        //   'shop_id' => $data['shop_id'],
        // ];
        // $this->productModel->add($productData); // 为了验证
        if ($res) {
            // 插入到队列中
            $this->upThree($insertData['ordersno']);
            return $this->returnMsg(200, '上传成功');
        }
        return $this->returnMsg(1, '上传失败');
    }

    //修改 用户修改
    public function editUser($where, $data)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        $optionalFields = $this->newOrdersInfo->getField();

        $field = $this->getRealData($data, [], $optionalFields, []);
        if (isset($data['status']) && ($data['status'] == 0)) {
            $field['status'] = 0;
        }
        $isTrue = $this->getOne($where);
        if (!$isTrue) {
            return $this->returnMsg(3, '您无权限修改订单信息');
        }
        $isEditData = ''; // 未修改
        $isedit = false;
        if (!empty($field['pay_identity'])) {
            $str = '用户修改';
            $isEditData .= '1+'; // 用户信息修改
        }
        foreach ($field as $key => $v) {
            // 用户信息 + 商品信息 + 产品信息
            if ($key == 'goods_name' || $key == 'product_name' || $key == 'status_desc' || $key == 'pay_name' || $key == 'pay_phone' || $key == 'pay_identity' || $key == 'post_province' || $key == 'post_city' || $key == 'post_area' || $key == 'post_address' || $key == 'shop_name' || $key == 'remark') {
                if ($isTrue[$key] != $v) {
                    $isedit = true;
                    if ($key == 'shop_name') {
                        $str .= '分销商修改成' . $v;
                    } else {
                        $str .= $data->user->user_name . '将' . $isTrue[$key] . '修改为' . $v;
                    }
                }
            }
        }

        // 记录 修改日志
        // 添加操作日志
        $logDdata = [
            'orderno' => !empty($field['orderno']) ? $field['orderno'] : $isTrue->orderno,
            'remark' => date('Y-m-d H:i:s') . $str,
        ];
        $this->logModel->add($logDdata); // 存入操作日志
        $editOrderWhere = $where;
        $updateOrder = [];
        if (!empty($isEditData)) {
            $updateOrder = strpos($isTrue['is_edit'], $isEditData) ? $isTrue['is_edit'] : ($isTrue['is_edit'] . $isEditData);
        }
        if (!empty($updateOrder)) {
            $this->model->where($where)->update(['is_edit' => $updateOrder]);
        }
        $whereUser = [
            'id' => $isTrue['orders_user_id']
        ];
        // 修改的是用户信息
        return $this->newOrdersInfo->edit($whereUser, $data);
    }

    /**
     * Undocumented function
     *
     * @param [type] $where 修改的条件
     * @param [type] $data  修改的数据
     * @param boolean $isMust 是否返回数据
     * @param boolean $isEditTrue 是否修改上游提交的数据
     * @return void
     */
    public function edit($where, $data, $isMust = false, $isEditTrue = true, $isGoodsZTyep = 2)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        $optionalFields = $this->model->getTableField();

        $field = $this->getRealData($data, [], $optionalFields, []);
        if (isset($data['status']) && ($data['status'] == 0)) {
            $field['status'] = 0;
        }
        // 查询条件
        $isTrues = $this->getOne($where);  // 查询出来的应该第一个
        if (!$isTrues) {
            return $this->returnMsg(3, '您无权限修改订单信息');
        }
        $whereEdit = ['orders_user_id' => $isTrues['orders_user_id']];
        $isTrue = $this->getOne($whereEdit);
        if (!$isTrue) {
            $isTrue = $isTrues;
        }
        // 修改商品 修改商品 或修改
        $str = ''; // 将某某数据修改成了 某某数据
        $isSubmit = false;
        $isEditData = ''; // 未修改
        $isedit = false;
        if (!empty($field['pay_identity'])) {
            $str = '用户修改';
            $isEditData .= '1+'; // 用户信息修改
        }
        if (!empty($field['goods_id'])) {
            $str = '商品信息修改';
            $isSubmit = true; // 商品信息修改
            $isEditData .= '2+';
        }
        if (!empty($field['product_id'])) {
            $str = '产品修改'; // 产品修改
            $isSubmit = true;
            $isEditData .= '3+';
        }
        if (!empty($field['goods_id'])) {
            // $resGoods = $this->goodsModel->where(['id' => $field['goods_id']])->first();
            // 商品得上架
            $resGoods = $this->goodsModel->getOne(['id' => $field['goods_id']]);
            if (empty($resGoods)) {
                return $this->returnMsg(106, '未查询到商品');
            }
            if ($resGoods->status != 0) {
                return $this->returnMsg(106, '商品已下架,请上架商品后在提交');
            }
            $field['goods_id'] = $resGoods->id;
            $field['goods_code'] = $resGoods->goods_code;
            $field['goods_name'] = $resGoods->name;
            $field['product_id'] = $resGoods->product_id;
            $field['product_name'] = $resGoods->product_name;
            $field['product_code'] = $resGoods->product_code;
            $field['sup_shop_name'] = $resGoods->sup_shop_name;
            $field['shop_name_str'] = $resGoods->shop_name_str;
            $field['sup_goods_name'] = $resGoods->sup_goods_name;
            $field['shop_id_str'] = $resGoods->shop_id_str;
            $field['sup_goods_id_str'] = $resGoods->sup_goods_id_str;
            $field['sup_shop_id'] = $resGoods->sup_shop_id;
            $field['shop_name'] = $resGoods->shop_name;
            $field['shop_str'] = $resGoods->shop_str;
            // $field['sup_goods_id'] = $resGoods->sup_goods_id;
            $field['sup_goods_id'] = $isTrue['goods_id'];
            $field['sup_goods_name'] = $isTrue['goods_name'];
            $field['sup_goods_code'] = $isTrue['goods_code'];
            $field['sup_product_code'] = $isTrue['product_code'];
            $field['sup_product_name'] = $isTrue['product_name'];
            $field['sup_product_id'] = $isTrue['product_id'];
            $field['one_product_code'] = $isTrue['one_product_code'];
            $field['one_product_name'] = $isTrue['one_product_name'];
            $field['one_product_id'] = $isTrue['one_product_id'];
            // 关联到产品信息
            if (!empty($resGoods->sup_goods_id_str)) {
                $resGoodsArr = explode('.', $resGoods->sup_goods_id_str);
                if (!empty($resGoodsArr[1])) {
                    $resGoodsProduct = $this->goodsModel->getOne(['id' => $resGoodsArr[1]]);
                    $field['product_id'] = $resGoodsProduct->product_id;
                    $field['product_name'] = $resGoodsProduct->product_name;
                    $field['product_code'] = $resGoodsProduct->product_code;
                }
            }
            // var_dump($field, 444444444);
            // 需要将上游信息清空  是否清空
            // 如果修改的是商品的话 不需要清空上游信息的
            if ($isEditTrue) {
                $field['sup_id'] = 0;
                $field['sup_name'] = '';
                $field['sup_orderno'] = '';
            }

            // var_dump($field);
        }
        if (isset($field['status'])) {
            $isedit = true;
        }
        if (isset($field['status'])) {
            $field['status_desc'] = $this->model->getOrderStatusDesc($field['status']);
        }
        // var_dump(json_decode(json_encode($isTrue), true), 222222222);
        // var_dump($field, 111111111);
        foreach ($field as $key => $v) {
            // 用户信息 + 商品信息 + 产品信息
            if ($key == 'goods_name' || $key == 'product_name' || $key == 'status_desc' || $key == 'pay_name' || $key == 'pay_phone' || $key == 'pay_identity' || $key == 'post_province' || $key == 'post_city' || $key == 'post_area' || $key == 'post_address' || $key == 'shop_name' || $key == 'remark') {
                if ($isTrue[$key] != $v) {
                    $isedit = true;
                    if ($key == 'shop_name') {
                        $str .= '分销商修改成' . $v;
                    } else {
                        $str .= !empty($data->user->user_name) ? $data->user->user_name : '' . '将' . $isTrue[$key] . '修改为' . $v . '<br/>';
                    }
                }
            }
        }
        if (!empty($field['pay_identity'])) {
            // 用户修改
            $whereEdit = [
                'id' => $isTrue['orders_user_id']
            ];
            $this->newOrdersInfo->edit($whereEdit, $data);
        }
        // var_dump($field);
        // die();
        if (!$isedit) {
            $ordernum = $isTrue['ordersno'];

            dispatch(new OrderSubmit($ordernum, 'smsAndPushOrder'))->onQueue('TaskOrderSubmitDispose');

            return $this->returnMsg(200, '无修改任何数据');
        }
        // 添加操作日志
        $logDdata = [
            'orderno' => !empty($field['orderno']) ? $field['orderno'] : $isTrue['orderno'],
            'remark' => date('Y-m-d H:i:s') . $str,
        ];
        if (!empty($isEditData)) {
            $field['is_edit'] = strpos($isTrue['is_edit'], $isEditData) ? $isTrue['is_edit'] : ($isTrue['is_edit'] . $isEditData);
        }
        if (!empty($field['orderno'])) {
        }

        $this->logModel->add($logDdata); // 存入操作日志
        // if (isset($field['ordersno'])) {
        //   // unset($field['orderno']);
        // }
        // $field = array_merge($field, $isTrue);
        $nums = $isTrue['nums'] + 1;
        $field['ordersno'] = $isTrue['orderno'] . '-' . $nums;
        $field['orders_user_id'] = $isTrue['orders_user_id'];
        $field['nums'] = $nums;
        $field['type'] = $isGoodsZTyep;
        $field['shop_id'] = $isTrue['shop_id'];
        $field['pay_name'] = $isTrue['pay_name'];
        $field['pay_phone'] = $isTrue['pay_phone'];
        $field['pay_identity'] = $isTrue['pay_identity'];
        $field['post_address'] = $isTrue['post_address'];

        if (!empty($nums) && $nums > 5) {
            $field['status'] = 60;
            $field['three_msg'] = '转发超出限制';
        }
        // var_dump($field);
        // die();
        // var_dump($field, 1111111);
        // $res = $this->model->where($where)->update($field); //修改订单表
        $res = $this->addEdit($field); //直接生成一条新的数据订单表 // 修改或新添加
        // var_dump($res, 444444444);
        if ($isSubmit) {
            //重新提交产品
        }
        if ($isMust && $res) {
            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    public function editStatus($where, $data)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        $optionalFields = $this->model->getTableField();

        $field = $this->getRealData($data, [], $optionalFields, []);
        if (!empty($field['status'])) {
            $field['status_desc'] = $this->model->getOrderStatusDesc($field['status']);
        }
        $res = $this->model->where($where)->update($field);
        if ($res) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(100, '修改失败');
    }

    // 系统修改  修改订单的物流信息 和订单状态
    public function editThree($where, $data, $isChannel = true)
    {

        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        $wheres = [];
        if (!empty($where['orderno'])) {
            // $where['ordersno'] = $where['orderno'];
            $wheres['ordersno'] = $where['orderno'];
            unset($where['orderno']);
        }
        $wheres = array_merge($where, $wheres);
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);
        //先查询是否存在
        $isTrue = $this->getOne($wheres);
        if (!$isTrue) {
            return $this->returnMsg(3, '您无权限修改订单信息');
        }
        // 防止重复修改订单状态   1 50
        if (!empty($field['status']) && !empty($isTrue['status']) && $field['status'] <= $isTrue['status'] && $isTrue['status'] <= 40) {
            return $this->returnMsg(200, '该状态已提交过了');
        }
        // var_dump($isChannel, 5555555555);
        // 将
        if (!empty($field['status']) && $field['status'] >= 50 && !$isChannel) {

            // 提交到第三方 重新提交到三方
            dispatch(new OrderSubmit($isTrue['ordersno'], 'createOrder'))->onQueue('TaskOrderSubmitDispose');
        }

        $str = '修改';
        if (!empty($field['status'])) {
            $field['status_desc'] = $this->model->getOrderStatusDesc($field['status']);
            $str .= $field['status_desc'];
        }


        $logDdata = [
            'orderno' => $isTrue['orderno'],
            'remark' => date('Y-m-d H:i:s') . $str,
        ];
        // var_dump($logDdata);
        $this->logModel->add($logDdata); // 存入操作日志
        // 修改  // 记录时间  不用设置 提交到第三方的时间
        // if (!empty($field['status']) && $field['status'] == 1) {
        //   $field['three_time_at'] = date('Y-m-d H:i:s');
        // }
        if (!empty($field['status']) && $field['status'] == 20) {
            $field['deliver_at'] = date('Y-m-d H:i:s');
        }
        if (!empty($field['status']) && $field['status'] == 30) {
            $field['sign_at'] = date('Y-m-d H:i:s');
        }
        if (!empty($data['activat_at'])) {
            $field['activat_at'] = $data['activat_at'];
        }
        if (!empty($field['status']) && $field['status'] == 40 && empty($field['activat_at'])) {
            $field['activat_at'] = date('Y-m-d H:i:s');
        }
        if (empty($field['activat_at'])) {
            unset($field['activat_at']);
        }

        $res = $this->model->where($wheres)->update($field); //修改订单表
        // $field['order_id'] = $isTrue->id;

        // $this->model->edit(['order_id' => $isTrue->id], $field); // 为了统计
        if ($res) {
            return $this->returnMsg(200, '修改成功', ['orderno' => $isTrue['ordersno']]);
        }
        return $this->returnMsg(101, '修改失败');
    }

    //列表查询
    public function lists($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);

        return $res;
    }

    public function getOne($where, $field = ['*'])
    {
        // 查询到的是 最后一个数据  例如条件是: orderno 查询到的是最后一个
        if (!empty($where['ordersno'])) {
            $where['orders_old.ordersno'] = $where['ordersno'];
            unset($where['ordersno']);
        }
        if (!empty($where['orderno'])) {
            $where['orders_user_old.orderno'] = $where['orderno'];
            unset($where['orderno']);
        }
        if (!empty($where['dist_orderno'])) {
            $where['orders_user_old.dist_orderno'] = $where['dist_orderno'];
            unset($where['dist_orderno']);
        }
        // var_dump($where);
        $res = $this->model->where($where)->select($field)->orderBy('orders_old.id', 'desc')->leftJoin('orders_user_old', 'orders_user_old.id', '=', 'orders_old.orders_user_id')->first();
        if (($res)) {
            return json_decode(json_encode($res), true);
        }
        return $res;
    }

    //下单
    public function orderSubmit()
    {
    }
    // 处理订单的状态
    // 订单的状态 1: 返回的物流等信息 修改 2: 状态修改
    public function editOrderStatus($data)
    {
    }

    // 获取订单状态 和来源
    public function getOrderStatus()
    {
        $data = $this->model->orderStatus();
        $datas = [];
        foreach ($data as $key => $v) {
            $datas[] = [
                'id' => $key,
                'value' => $v
            ];
        }
        return $this->returnMsg(200, '查询成功', $datas);
    }

    public function getOneOrderStatus($status = '')
    {
        $data = $this->model->orderStatus();
        if (isset($status)) {
            return $data[$status];
        }
        return $data;
    }

    // 来源
    public function getOrderSource()
    {
        $data = $this->model->getSourceType();
        $datas = [];
        foreach ($data as $key => $v) {
            $datas[] = [
                'id' => $key,
                'value' => $v
            ];
        }
        return $this->returnMsg(200, '查询成功', $datas);
    }

    // 来源解释
    public function getOneOrderSource($type)
    {
        $data = $this->model->getSourceType();
        if (isset($type)) {
            return $data[$type];
        }
        return $data;
    }

    //异步的时候 提交到第三方
    public function upThree($ordernum = '', $type = '')
    {
        if (empty($type)) {
            dispatch(new OrderSubmit($ordernum, 'createOrder'))->onQueue('TaskOrderSubmitDispose');
        }
        if (!empty($type)) {
            $time = 60 * 60 * 4;
            // Queue::later(Carbon::now()->addMinutes(10), new OrderSubmit($ordernum, 'createOrder'));
            dispatch(new OrderSubmit($ordernum, 'createOrder', $time))->onQueue('TaskOrderSubmitDispose');
        }
        return 1;
    }

    // 修改分销商  订单修改分销商的操作
    public function editDistAll($array = [])
    {
        if (empty($array['dist_id'])) {
            return $this->returnMsg(101, '分销商不能为空');
        }
        // 1: 检验是否是分销商
        // 2:
        if (empty($array['goodsAllid'])) {
            return $this->returnMsg(102, '修改订单信息不能为空');
        }
        foreach ($array['goodsAllid'] as $key => $v) {
            $where = [
                'orders.id' => $v
            ];
            // 查询的是订单
            $ordersArr = $this->getOne($where);
            $goods_id = $ordersArr['goods_id'];
            $res = $this->editDistOne($v, $array['dist_id'], $goods_id);
            if (!empty($res['code']) && $res['code'] != 200) {
                break;
            }
        }
        if (!empty($res)) {
            return $res;
        }
        return $this->returnMsg(200, '修改成功', []);
    }

    /**
     * Undocumented function
     *
     * @param [type] $dist_id 分销商id
     * @param [type] $goods_id  供应商的商品id
     * @return void
     */
    public function editDistOne($orderid, $dist_id, $goods_id, $editData = [])
    {
        // 查询分销商的商品
        $where = [
            'shop_id' => $dist_id,
            'sup_goods_id' => $goods_id
        ];
        // 查询出来商品信息
        // var_dump($where);

        $goodsData = $this->goodsModel->getOne($where);
        // var_dump($goodsData);
        if (!($goodsData)) {
            return $this->returnMsg(100, '未查询到商品信息');
        }
        // 然后修改商品信息
        $update = [
            'shop_id' => $dist_id,
            'goods_id' => $goodsData->id
        ];
        if (!empty($editData)) {
            $update = array_merge($update, $editData);
        }
        // 注意下商品
        // var_dump($update, 111111111);
        $editWhere = [
            'orders.id' => $orderid
        ];
        $res = $this->edit($editWhere, $update, false, false);
        return $res;
    }

    // 处理老数据
    public function editOrderUpOrders($data)
    {
        if (empty($data['ordersno'])) {
            $data['ordersno'] = $data['orderno'];
        }
        if (!empty($data['orderno'])) {
            $data['ordersno'] = $data['orderno'];
        }
        $mustField = [
            'ordersno',
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'ordersno' => '订单编号不能为空',
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        // if (!empty($insertData['status'])) {
        //   $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
        // }
        // if (!empty($insertData['source'])) {
        //   $insertData['source_type'] = $this->model->getOrderSource($insertData['source']);
        // }
        // 启动 事务
        // DB::beginTransaction();
        // 订单表插入
        if (!empty($insertData['status'])) {
            $insertData['status_desc'] = $this->model->getOrderStatusDesc($insertData['status']);
        }
        // if (!empty($insertData['shop_id_str'])) {
        //   // $insertData['shop_id_str'] = substr_replace('0.', '', $insertData['shop_id_str'], 1);
        //   $insertData['shop_id_str'] = substr_replace($insertData['shop_id_str'], '', 0, 2);
        // }
        if (empty($insertData['shop_id_str'])) {
            $insertData['shop_id_str'] = $insertData['shop_id'];
        }
        // if (!empty($insertData['sup_goods_id_str'])) {
        //   // $insertData['sup_goods_id_str'] = substr_replace('0.', '', $insertData['sup_goods_id_str'], 1);
        //   $insertData['sup_goods_id_str'] = substr_replace($insertData['sup_goods_id_str'], '', 0, 2);
        // }
        // if (empty($insertData['sup_goods_id_str'])) {
        //   $insertData['sup_goods_id_str'] = $insertData['goods_id'];
        // }
        // if (!empty($insertData['shop_name_str'])) {
        //   // $insertData['shop_name_str'] = substr_replace('0.', '', $insertData['shop_name_str'], 1);
        //   $insertData['shop_name_str'] = substr_replace($insertData['shop_name_str'], '', 0, 2);
        // }
        if (empty($insertData['shop_name_str'])) {
            $insertData['shop_name_str'] = $insertData['shop_name'];
        }
        if (empty($insertData['created_at'])) {
            $insertData['created_at'] = date('Y-m-d H:i:s');
        }
        if (!empty($insertData['product_code'])) {
            $insertData['one_product_code'] = !empty($insertData['product_code']) ? $insertData['product_code'] : null;
        }
        if (!empty($insertData['product_name'])) {
            $insertData['one_product_name'] = !empty($insertData['product_name']) ? $insertData['product_name'] : null;
        }
        if (!empty($insertData['product_id'])) {
            $insertData['one_product_id'] = !empty($insertData['product_id']) ? $insertData['product_id'] : null;
        }
        $ordersId = $this->model->insertGetId($insertData);
        // 先添加 orders 然后在添加 orders_info
        $data['orders_id'] = $ordersId;
        // 记录用户的信息
        $userArr = $this->newOrdersInfo->add($data);
        $userId = 0;
        if (!empty($userArr['code']) && $userArr['code'] == 200) {
            $userId = $userArr['data']['id'];
        }
        if (empty($userArr['code']) || $userArr['code'] != 200) {
            return $userArr;
        }
        // 将 order里面存入 信息
        $res = $this->model->where(['id' => $ordersId])->update(['orders_user_id' => $userId]);
        if ($res) {
            // 插入到队列中
            // $this->upThree($insertData['ordersno']);
            // DB::commit();
            return $this->returnMsg(200, '上传成功');
        }
        // DB::rollback();
        return $this->returnMsg(1, '上传失败');
    }

    public function varOrderUpOrders($data)
    {
        $where = [];
        if (!empty($data['orderno'])) {
            $where['orders.ordersno'] = $data['orderno'];
        }
        $isTrue = $this->getOne($where);
        // var_dump($isTrue);
        if (empty($isTrue)) {
            return $this->editOrderUpOrders($data);
        }
        $whereUser = ['id' => $isTrue['orders_user_id']];
        // 判断创建时间
        $isUser = $this->newOrdersInfo->getOne($whereUser);
        $isUser = json_decode(json_encode($isUser), true);
        // var_dump($isUser);
        $isEditUser = false;
        $res = ['status' => '200', 'msg' => '数据没问题'];
        if (empty($isUser)) {
            $res = ['status' => '200', 'msg' => '数据遗漏'];
        }
        if (!empty($isUser)) {
            if (!empty($data['dist_orderno']) && $isUser['dist_orderno'] != $data['dist_orderno']) {
                // 说明这条数据有问题
                // $this->newOrdersInfo->edit($whereUser, $data);
                $isEditUser = true;
            }
            if ($isUser['pay_identity'] != $data['pay_identity']) {
                $isEditUser = true;
            }
            if (strtotime($data['created_at']) != strtotime($isUser['created_at'])) {
                $isEditUser = true;
            }
            // var_dump($isEditUser);
            if ($isEditUser) {
                $res = $this->newOrdersInfo->edit($whereUser, $data);
            }
        }

        return $res;
        if ($res) {
            // 插入到队列中
            // $this->upThree($insertData['ordersno']);
            return $this->returnMsg(200, '上传成功');
        }
        return $this->returnMsg(1, '上传失败');
    }

    // 按照订单号 处理 如果超过两条 执行删除
    public function isManyOrder($where)
    {
        return $this->model->where($where)->count();
    }
}
